<template>
  <el-form
    ref="deptForm"
    label-width="120px"
    :model="form"
    :rules="rules"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="$emit('close')">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getEmpList } from '@/api/employees'
import {
  addDepartments,
  getDepartDetail,
  updateDepartments
} from '@/api/departments'
export default {
  // 接收传递过来的id
  props: {
    id: {
      type: String, // 类型
      required: true // 必传项
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    originList: {
      required: true,
      type: Array
    }
  },
  data() {
    const validateCode = (rule, value, callback) => {
      // 做添加
      let codeList = this.originList.map(item => item.code)
      console.log(value, '输入的code')
      console.log(codeList, '比较的数据codeList')

      // 做编辑
      if (this.isEdit) { // 排除当前编辑的部门   取出code
        codeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
        console.log(value, '编辑输入的code')
        console.log(codeList, '编辑比较的数据codeList')
      }

      if (codeList.includes(value)) {
        callback(new Error('编码重复'))
      } else {
        callback()
      }
    }

    // 同级之间 部门名称不能重复
    const validateName = (rule, value, callback) => {
      // 排除上下级之间  取出name   item.pid===this.id 筛选出同级元素
      let nameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      console.log('nameList,同级元素', nameList)

      // 做编辑
      // 被占用的部门名称-当前兄弟部门-它自己名字
      if (this.isEdit) {
        //  根据id找到  pid
        const pid = this.originList.find(item => item.id === this.id).pid
        // 根据pid 找到兄弟部门  item.id !== this.id 排除自身
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id).map(item => item.name)
        console.log(value, '编辑name输入的code')
        console.log(nameList, '编辑name比较的数据nameList')
      }
      if (nameList.includes(value)) {
        callback(new Error('同级名称不能重复'))
      }
      callback()
    }

    return {
      // 定义表单对象
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      list: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadEmployee()
    // 如果当前是编辑状态 发送获取部门详情
    if (this.isEdit) {
      this.loadDetail() // 编辑状态 调用接口
    }
  },
  methods: {
    async loadEmployee() {
      const res = await getEmpList()
      console.log(res.data)
      this.list = res.data
    },
    // 合并表单对象 传参   表单对象+id
    async add() {
      // 子传父 关闭弹窗 重新请求数据
      await addDepartments({ ...this.form, pid: this.id })
      this.$emit('success')
    },
    async edit() {
      const res = await updateDepartments(this.form)// 表单中含有id
      console.log(res, '修改数据')
      this.$emit('success') // 重新请求数据
    },
    // 点击添加 /修改
    hSubmit() {
      this.$refs.deptForm.validate(val => {
        if (val) {
          this.isEdit ? this.edit() : this.add()
        }
      })
    },

    // 编辑功能 调用接口 回填数据
    async loadDetail() {
      const res = await getDepartDetail(this.id) // 后台需要传递id
      console.log(res, '编辑功能')
      // 数据回填
      this.form = res.data
    }
    // 事件监听 获取详情
    // watch: {
    //    id(newval) {
    //      console.log(newval)
    //      this.loadDetail()
    //    }
    // }

    // 重置表单 优化
    //  resetForm(){
    //    this.$refs.deptForm.resetFields()
    //  }

  }
}
</script>
